<?php
/*		$tfidf = $term_frequency *  // tf
			log( $total_document_count / $documents_with_term, 2); // idf */		

class VectorSpace {

	private $subjective;
	private $objective;
	private $subjectiveIndex;
	private $objectiveIndex;

	public function __construct() {
		echo "start at ".date("D M j G:i:s T Y")."<br />";
		$this -> subjectiveIndex = json_decode(file_get_contents("./src/dsSubj.txt"), true);
		$this -> objectiveIndex = json_decode(file_get_contents("./src/dsObj.txt"), true);
		echo "end at ".date("D M j G:i:s T Y")."<br />";
	}
	
	function getIndex($collection) {
		$dict = array();
		$docCount = array();
		// for each document in the collection
		foreach($collection as $docID => $terms) {
			// take the lenght of the document
			$docCount[$docID] = count($terms);
			
			// for each word in the document
			foreach($terms as $term) {
				// add a new array for a new word
				if(!isset($dict[$term])) {
					$dict[$term] = array('df' => 0, 'postings' => array());
				}
				// increment the term frequency in the collection (df)
				if(!isset($dict[$term]['postings'][$docID])) {
					$dict[$term]['df']++;
					$dict[$term]['postings'][$docID] = array('tf' => 0);
				}
				// increment the term frequenxy in the specific document
				$dict[$term]['postings'][$docID]['tf']++;
			}
		}
//		echo 'FINAL: '.count($dict).'<br>';

		$result = array('docCount' => $docCount, 'dictionary' => $dict);

		return $result;
	}
	
	
	function getTfidf($term) {
		$index = $this -> subjectiveIndex;
//		$index = $this -> objectiveIndex;
		$docCount = count($index['docCount']);
		$entry = $index['dictionary'][$term];
		foreach($entry['postings'] as  $docID => $postings) {
			echo "Document $docID and term $term give TFIDF: " .
					($postings['tf'] * log($docCount / $entry['df'], 2));
			echo "<br>";
		}
	}
	
	function normalise($doc) {
		foreach($doc as $entry) {
			$total += $entry*$entry;
		}
		$total = sqrt($total);
		foreach($doc as &$entry) {
			$entry = $entry/$total;
		}
		return $doc;
	}
	
	function cosineSim($docA, $docB) {
		$result = 0;
		foreach($docA as $key => $weight) {
			$result += $weight * $docB[$key];
		}
		return $result;
	}
	
	public function main() {
		
		// here is the document to query for
//		$query = array('is', 'short', 'string');
//		$query = array('smart', 'and', 'alert', 'thirteen', 'conversations', 'about', 'one', 'thing', 'is', 'a', 'small', 'gem');
		$query = explode(' ', 'they\'re jewish they\'re grandmothers and they\'re lesbians');
		
				
		// take the index
		$index = $this -> subjectiveIndex;
		$matchDocs = array();
		
		// take the total number of documents in the collection
		$docCount = count($index['docCount']);
		
		// for each term in the query, find the tf-idf value for each document in the collection,
		// normalizing the tf-idf value found for each document basing on the lenght of that document
		foreach($query as $qterm) {
			$entry = $index['dictionary'][$qterm];
			foreach($entry['postings'] as $docID => $posting) {
				if(!isset($matchDocs[$docID])) {
				$matchDocs[$docID] = 0;
				}
//				$matchDocs[$docID] += ($posting['tf'] / $index['docCount'][$docID] ) * log(($docCount + 1) / ($entry['df'] + 1), 2);
				$matchDocs[$docID] += $posting['tf'] * log(($docCount + 1) / ($entry['df'] + 1), 2);
			}
		}
		
		$doc = $matchDocs;
		$doc1 = $matchDocs;
		
		// length normalise
		foreach($matchDocs as $docID => $score) {
			$matchDocs[$docID] = $score/$index['docCount'][$docID];
		}

		$total = 0;
		foreach($doc as $docID => $entry) {
			$total += $entry*$entry;
		}
		$total = sqrt($total);
		foreach($doc as $docID => &$entry) {
			$entry = $entry/$total;
		}

		$total = 0;
		$max = -100000;
		foreach($doc1 as $docID => $entry) {
			if ($entry > $max)
				$max = $entry;
				
			$total += $entry;
		}
		$avg = $total/count($doc1);
		foreach($doc1 as $docID => &$entry) {
			$entry = ($entry - $avg)/$max;
		}
		arsort($doc1); // high to low
		foreach ($doc1 as $docID => $curr) {
				echo "<pre>Document $docID -> $curr</Pre>";
		}

		arsort($matchDocs); // high to low
		arsort($doc); // high to low
		echo "<BR> SUBJECTIVE <BR>";
		$i = 0;
		foreach ($matchDocs as $docID => $curr) {
			if ($i < 5)
				echo "<pre>Document $docID -> $curr</Pre>";
			else
				break;
			$i++;
		}
		echo "<BR> SUBJECTIVE 2<BR>";
		$i = 0;
		foreach ($doc as $docID => $curr) {
			if ($i < 5)
				echo "<pre>Document $docID -> $curr</Pre>";
			else
				break;
			$i++;
		}

		echo "<BR> OBJECTIVE <BR>";
		$index = $this -> objectiveIndex;
		$matchDocs = array();
		$docCount = count($index['docCount']);
		
		foreach($query as $qterm) {
			$entry = $index['dictionary'][$qterm];
			foreach($entry['postings'] as $docID => $posting) {
				if(!isset($matchDocs[$docID])) {
				$matchDocs[$docID] = 0;
				}
				$matchDocs[$docID] += $posting['tf'] * log(($docCount + 1) / ($entry['df'] + 1), 2);
			}
		}
		
		$doc = $matchDocs;
		$doc1 = $matchDocs;

		// length normalise
		foreach($matchDocs as $docID => $score) {
			$matchDocs[$docID] = $score/$index['docCount'][$docID];
		}

		$total = 0;
		foreach($doc as $docID => $entry) {
			$total += $entry*$entry;
		}
		$total = sqrt($total);
		foreach($doc as $docID => &$entry) {
			$entry = $entry/$total;
		}


$i = 0;
		arsort($matchDocs); // high to low
		arsort($doc); // high to low
		foreach ($matchDocs as $docID => $curr) {
			if ($i < 5)
				echo "<pre>Document $docID -> $curr</Pre>";
			else
				break;
			$i++;
		}
$i = 0;
		echo "<BR> OBJECTIVE 2<BR>";
		foreach ($doc as $docID => $curr) {
			if ($i < 5)
				echo "<pre>Document $docID -> $curr</Pre>";
			else
				break;
			$i++;
		}


		$total = 0;
		$max = -100000;
		foreach($doc1 as $docID => $entry) {
			if ($entry > $max)
				$max = $entry;
				
			$total += $entry;
		}
		$avg = $total/count($doc1);
		foreach($doc1 as $docID => &$entry) {
			$entry = ($entry - $avg)/$max;
		}
		arsort($doc1); // high to low
		foreach ($doc1 as $docID => $curr) {
				echo "<pre>Document $docID -> $curr</Pre>";
		}


//		var_dump($matchDocs);
	}


	function getCollection() {
		$this -> objective = json_decode(file_get_contents("./src/dataSetObj.txt", true));
		$this -> subjective = json_decode(file_get_contents("./src/dataSetSubj.txt", true));
	}

}
?>









